// 设置文本
const setText = (context, fs, color, x, y, c) => {
  context.setFontSize(fs);
  context.setFillStyle(color);
  context.setTextAlign('left');
  context.fillText(c, x, y);
  context.restore();
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign_img:"/img/add.png",
    detail:{},
    imagePath:""
  },
  // 查看图片
  showImg(e){
     let {img}   =   e.currentTarget.dataset||{};
     wx.previewImage({
       urls: [img],
       current:img
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    let electron_print = null, sign_img=null,self=this;
    var ctx = wx.createCanvasContext('firstCanvas');
    let data = await wx.$api.getFacilitatorInfo({ enterprise_professional_facilitator_id:options.id})
       let data_=data.data.data;
       this.setData({
         detail: data_||{}
       })
    if (!data_.have_contract_img){
       wx.downloadFile({
         url: data_.sign_img,
         success:res1=>{
           sign_img = res1.tempFilePath;
           wx.downloadFile({
             url: data_.electron_print,
             success: res2 => {
               electron_print = res2.tempFilePath;
               if (electron_print && sign_img){
                      
                // 绘制区域
                 const grd = ctx.createLinearGradient(0, 0, 0, 650);
                 grd.addColorStop(0, '#ffffff');
                 grd.addColorStop(1, '#ffffff');
                 ctx.setFillStyle(grd);
                 ctx.save();

                 // 垂直渐变
                 setText(ctx, 16, '#000000', (400 - ctx.measureText('合同签署信息').width) * 0.5, 400 * 0.1, '合同签署信息');
                 setText(ctx, 14, '#000000', 40, 100, '合同编号:');
                 setText(ctx, 14, '#000000', 130, 100, data_.enterprise_professional_facilitator_sn);

                 setText(ctx, 14, '#000000', 40, 135, '签署内容:');
                 setText(ctx, 14, '#000000', 130, 135, '诚信纳税承诺书-' + data_.name);
                 setText(ctx, 14, '#000000', 130, 155, '税务办理授权委托书-' + data_.name);
                 setText(ctx, 14, '#000000', 130, 175, '自由职业合作服务协议-' + data_.name);


                 setText(ctx, 14, '#000000', 40, 220, '签署日期:');
                 setText(ctx, 14, '#000000', 130, 220, data_.sign_time);


                 setText(ctx, 16, '#000000', (400 - ctx.measureText('签署事件记录').width) * 0.5, 280, '签署事件记录');

                 setText(ctx, 14, '#000000', 40, 330, '姓名/名称:');
                 setText(ctx, 14, '#000000', 130, 330, data_.facilitator_name);


                 setText(ctx, 14, '#000000', 40, 380, '签名:');
                 ctx.drawImage(electron_print, 130, 350, 60, 60)


                 setText(ctx, 14, '#000000', 40, 450, '签署日期:');
                 setText(ctx, 14, '#000000', 130, 450, data_.sign_time);

                 setText(ctx, 14, '#000000', 40, 485, '姓名/名称:');
                 setText(ctx, 14, '#000000', 130, 485, data_.name);


                 setText(ctx, 14, '#000000', 40, 540, '签名:');
                 ctx.drawImage(sign_img, 130, 510, 60, 60)

                 setText(ctx, 14, '#000000', 40, 610, '签署日期:');
                 setText(ctx, 14, '#000000', 130, 610, data_.sign_time);
                 ctx.clip(); //剪切
                 ctx.save();
                 ctx.draw();
                 
                 self.saveImg(data_.enterprise_professional_facilitator_id);
               }
             }
           })
         }
       })
    }
  },
  saveImg(id) {
    let self = this;
    setTimeout(function () {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 400,
        height: 650,
        destWidth: 400 * 2,//生成图片的大小设置成canvas大小的二倍解决模糊
        destHeight: 650 * 2,
        canvasId: 'firstCanvas',
        quality: 1,
        fileType: 'png',
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          var nums = 0;
          var getFiles = () => {
            wx.getFileInfo({
              filePath: res.tempFilePath,
              success:  ress => {
                console.log("我在找", ress)
                let file_ =  ress.errMsg
                if (file_ == "getFileInfo:ok") {
                  wx.uploadFile({
                    url: wx.$config.uploadFile,
                    filePath: tempFilePath,
                    name: 'file',
                    success: async res => {
                      let data = JSON.parse(res.data)
                      if (data.data.url) {
                        self.setData({
                          imagePath: await data.data.url
                        });
                        wx.$api.upContractImg({
                          enterprise_professional_facilitator_id:id,
                          contract_img: await data.data.url
                        })
                      }
                    }
                  })
                } else {
                  getFiles();
                }
              }
            })
          }
          getFiles();
        }
      });
    }, 600);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})